package com.yc.framework.config;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;

/**
 * @Author wpp
 * @Date 2022/04/27/10:30
 * @Description
 */
//方式一
//public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
//    //对应yml文件中的配置
//    private String host;
//    private Integer port;
//    @Override
//    public RestHighLevelClient elasticsearchClient() {
//        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
//        RestHighLevelClient restHighLevelClient = new
//                RestHighLevelClient(builder);
//        return restHighLevelClient;
//    }
//}

//方式二
@Configuration
public class ElasticsearchConfig {

    @Value("${elasticsearch.url}")
    private String edUrl;

//localhost:9200 写在配置文件中就可以了

    @Bean
    RestHighLevelClient client() {

        ClientConfiguration clientConfiguration = ClientConfiguration.builder()

                .connectedTo(edUrl)//elasticsearch地址

                .build();

        return RestClients.create(clientConfiguration).rest();

    }

}




